<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="职能名称" prop="name">
        <el-input v-model="formItem.name" disabled />
      </el-form-item>
      <el-form-item label="管理部门" prop="orgCode">
        <el-input v-model="formItem.orgName" disabled />
      </el-form-item>
      <el-form-item label="交接项">
        <el-button size="mini" icon="el-icon-plus" @click="addItem">添加</el-button>
      </el-form-item>
      <el-row v-for="(item,index) in formItem.items" :key="index" :gutter="40">
        <el-col :span="10">
          <el-form-item label="项名称" :prop="'items.' + index + '.name'" :rules="rules.name">
            <el-input v-model="item.name" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="是否必填" :prop="'items.' + index + '.isRequired'" :rules="rules.isRequired">
            <el-select v-model="item.isRequired" filterable remote placeholder="请选择是否必填">
              <el-option v-for="(it) in typeList" :key="it.value" :label="it.label" :value="it.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-button type="danger" icon="el-icon-delete" circle @click="remove(index)" />
        </el-col>
      </el-row>
      <el-row :gutter="40" />
    </el-form>
  </div>
</template>

<script>
import { detail, update } from '@neu/dcom-ui/api/functional'

// 定义一个表单原始项
const formItem = {
  name: '',
  orgName: '',
  orgCode: null,
  users: [],
  items: []
}
export default {
  name: 'HandoverForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      typeList: [
        { label: '必填', value: true },
        { label: '非必填', value: false }
      ],
      descriptor: {},
      // 表单验证规则
      rules: {
        name: [{ required: true, message: '交接项不能为空', trigger: 'blur' }],
        isRequired: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
      }
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            resolve(update(this.id, this.formItem))
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    },
    remove(index) {
      this.formItem.items.splice(index, 1)
    },
    addItem() {
      this.formItem.items.push({ name: '', isRequired: false })
    }
  }
}
</script>
